﻿using BLL;
using models;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace yuejilalala
{
    public partial class search : System.Web.UI.Page
    {
        string name = "";
        int typeName = 0;
        int storeName = 0;
        ProductMarager Product = new ProductMarager();
        StoreMarager store = new StoreMarager();
        ProductTypeMarager ProductType = new ProductTypeMarager();
        CartMarager cartMarager = new CartMarager();
        public string dizhi = "";


        protected void Page_Load(object sender, EventArgs e)
        {
            Session["dizhi"] = "images/";
            dizhi = Session["dizhi"].ToString();
            userDl();
            if (!IsPostBack)
            {
                string name2 = Request.QueryString["name"];

                TextBox1.Text = name2;
                string name = Request.QueryString["name"];
                TextBox1.Text = name;
                Repeater4.DataSource = Product.GetProductCount3(TextBox1.Text);
                Repeater4.DataBind();
                if (name2 != null)
                {
                    Repeater4.DataSource = Product.GetProductCount3(TextBox1.Text);
                    Repeater4.DataBind();
                }
                else
                {
                    int id = 0;
                    if (Request.QueryString["typeid"]!=null)
                    {
                        id = int.Parse(Request.QueryString["typeid"]);
                    }
                   
                    if (id != 0)
                    {

                        Repeater4.DataSource = Product.GetProductCounts2(null, 0, id, null);
                        Repeater4.DataBind();
                    }

                    if (id != 0)
                    {
                        Repeater4.DataSource = Product.GetProductCounts2(null, id, 0, null);
                        Repeater4.DataBind();
                    }
                }
               
               
               

                Repeater2.DataSource = ProductType.GetProductCount2();
                Repeater2.DataBind();

                Repeater3.DataSource = store.StoreCount();
                Repeater3.DataBind();

                Repeater1.DataSource = Product.GetProductCount6();
                Repeater1.DataBind();
            }



        }

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string name1 = e.CommandArgument.ToString();
           name= name1;
            Repeater4.DataSource = Product.GetProductCounts2(TextBox1.Text, typeName, storeName, name);
            Repeater4.DataBind();
        }
        //Repeater4  一排四个
        protected int i = 0;
        protected void Repeater2_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (i % 4 == 0)
            {
                e.Item.Controls.Add(new LiteralControl("<div></div>"));
            }
            i++;
            //商品详情加载显示
            int typeName1 = int.Parse(e.CommandArgument.ToString());
            typeName= typeName1;
         
            Repeater4.DataSource = Product.GetProductCounts2(TextBox1.Text, storeName, typeName1, name);
            Repeater4.DataBind();
          
        }

        protected void Repeater3_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            int storeName1 = int.Parse(e.CommandArgument.ToString());
            storeName = storeName1 ;
            Repeater4.DataSource = Product.GetProductCounts2(TextBox1.Text, storeName1, typeName, name);
            Repeater4.DataBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Repeater4.DataSource = Product.GetProductCount3(TextBox1.Text);
            Repeater4.DataBind();
        }

        protected void Repeater4_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
           
            //进入商品详情
            if (e.CommandName == "spxq")
            {
                Response.Redirect("introduction.aspx?proid=" + e.CommandArgument);
            }
            //放如购物车
            if (e.CommandName == "jrgwc")
            {
                //将商品加入购物车
                if (Session["sessid"] == null)
                {
                    Response.Redirect("login.aspx");
                }
                //加入购物车
                //查询当前对应商品的内容
                List<Product> PList = Product.SelProduct(e.CommandArgument.ToString());
                //点击结算 生成订单 
                string id = Session["sessid"].ToString();//用户id
                                                         //然后将物品放入购物车
                List<Product> PLists = Product.SelProduct(e.CommandArgument.ToString());
                Cart cart = new Cart();
                cart.Count = int.Parse("1");//数量
                cart.Userid = int.Parse(id);//用户id
                cart.Storeid = PLists[0].Stroe;//店铺id
                cart.Proid = PLists[0].Id;//商品id
                if (cartMarager.addCart(cart))
                {
                    Response.Write("<script>alert('加入购物车成功！')</script>");
                    Response.Redirect("search.aspx");
                }
            }
        }
        protected int p = 0;
        protected void Repeater4_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (p %4== 0)
            {
                e.Item.Controls.Add(new LiteralControl("</li><li>"));
            }
            i++;
        }
        public void userDl()
        {
            if (Session["sess"] != null)
            {
                //用户存在则显示出来 并且隐藏注册和登陆显示
                User user = Session["sess"] as User;
                hl_dlm.Text = "用户名:" + user.UName;
                hl_dlm.Visible = true;//显示登陆名控件
                hl_dl.Visible = false;//请登陆隐藏
                hl_zc.Visible = false;//注册隐藏
            }

        }

    }
}

